package tongcheng.com.tongcheng.ui.yemian.shenfen.activity.install;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yanzhenjie.nohttp.RequestMethod;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import tongcheng.com.tongcheng.ConStans;
import tongcheng.com.tongcheng.R;
import tongcheng.com.tongcheng.adapter.InstallListAdapter;
import tongcheng.com.tongcheng.base.BaseActivity;
import tongcheng.com.tongcheng.bean.InstallListBean;
import tongcheng.com.tongcheng.network.CallServer;
import tongcheng.com.tongcheng.network.MyRequest;
import tongcheng.com.tongcheng.network.OnCallBack;
import tongcheng.com.tongcheng.ui.yemian.shenfen.activity.BaoZhenJinActivity;
import tongcheng.com.tongcheng.utils.GsonUtil;
import tongcheng.com.tongcheng.utils.Setting;
import tongcheng.com.tongcheng.utils.ShowToast;

public class InstallPeopleListActivity extends BaseActivity {

    @Bind(R.id.back)
    ImageView mBack;
    @Bind(R.id.title)
    TextView mTitle;
    @Bind(R.id.title_bar)
    RelativeLayout mBg;
    @Bind(R.id.installlist_search)
    EditText mInstalllistSearch;
    @Bind(R.id.installlist_cancle)
    TextView mInstalllistCancle;
    @Bind(R.id.installlist_ll1)
    LinearLayout mInstalllistLl1;
    @Bind(R.id.installlist_line)
    View mInstalllistLine;
    @Bind(R.id.installlist_rc)
    RecyclerView mInstalllistRc;
    @Bind(R.id.installlist_rc2)
    RecyclerView mInstalllistRc2;
    @Bind(R.id.installlist_rc4)
    RecyclerView mInstalllistRc4;
    @Bind(R.id.installlist_addpeople)
    Button mInstalllistAddpeople;
    @Bind(R.id.text1)
    TextView mTextView1;
    @Bind(R.id.text2)
    TextView mTextView2;
    @Bind(R.id.text4)
    TextView mTextView4;
    public List<InstallListBean.DataBean> data1 = new ArrayList<>();
    public List<InstallListBean.DataBean> data2 = new ArrayList<>();
    public List<InstallListBean.DataBean> data4 = new ArrayList<>();
    public List<InstallListBean.DataBean> datas = new ArrayList<>();
    private InstallListAdapter mInstallListAdapter;
    private InstallListAdapter mInstallListAdapter2;
    private InstallListAdapter mInstallListAdapter4;
    private LinearLayoutManager mLinearLayoutManager;
    private LinearLayoutManager mLinearLayoutManager2;
    private LinearLayoutManager mLinearLayoutManager4;

    @Override
    public Integer initResid() {
        return R.layout.activity_install_peoplelist;
    }

    @Override
    public void initCreate(Bundle savedInstanceState) {
        settitlebar(mBg);
        mTitle.setText("安装人员");
        mBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        mInstalllistRc.setNestedScrollingEnabled(false);
        mInstalllistRc2.setNestedScrollingEnabled(false);
        mInstalllistRc4.setNestedScrollingEnabled(false);
        mLinearLayoutManager = new LinearLayoutManager(this);
        mLinearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);

        mLinearLayoutManager2 = new LinearLayoutManager(this);
        mLinearLayoutManager2.setOrientation(LinearLayoutManager.VERTICAL);

        mLinearLayoutManager4 = new LinearLayoutManager(this);
        mLinearLayoutManager4.setOrientation(LinearLayoutManager.VERTICAL);

        mInstalllistRc.setLayoutManager(mLinearLayoutManager);
        mInstallListAdapter = new InstallListAdapter(this,data1);
        mInstalllistRc.setAdapter(mInstallListAdapter);



        mInstalllistRc2.setLayoutManager(mLinearLayoutManager2);
        mInstallListAdapter2 = new InstallListAdapter(this,data2);
        mInstalllistRc2.setAdapter(mInstallListAdapter2);



        mInstalllistRc4.setLayoutManager(mLinearLayoutManager4);
        mInstallListAdapter4 = new InstallListAdapter(this,data4);
        mInstalllistRc4.setAdapter(mInstallListAdapter4);

        mInstalllistAddpeople.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

               /* Intent intent = new Intent(InstallPeopleListActivity.this, InstallPeopleDetialActivity.class);
                intent.putExtra("id", 1);
               startActivity(intent);*/
                yanZheng();

            }
        });
    }

    //验证是否交过押金
    private void yanZheng() {
        MyRequest request = new MyRequest(ConStans.ShiFouJiaoGuoYaJinUrl, RequestMethod.GET);
        request.add("userid",Setting.getId());
        CallServer.getInstance().addGet(3, new OnCallBack() {
            @Override
            public void OnSuccess(int what, String json, Object ject) {
                Log.e("验证",json);
                try {
                    JSONObject object = new JSONObject(json);
                    if(object.getInt("status") == 1){
                        JSONObject object1 = object.getJSONObject("data");
                        int is_pledge = object1.getInt("is_pledge");
                        if(is_pledge == 1){//是
                            getInstallData();
                        }else if(is_pledge == 2){//否
                            ShowToast.showToast(object1.getString("code"));
                            startactivity(InstallPeopleListActivity.this, BaoZhenJinActivity.class);
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, request);
    }

    private void getInstallData() {
        MyRequest request = new MyRequest(ConStans.InstallPerson_Url);
        request.add("type", 5);
        request.add("userid", Setting.getId());
        CallServer.getInstance().addPost(this, request, 1, new OnCallBack() {
            @Override
            public void OnSuccess(int what, String json, Object ject) {
                Log.e("添加安装人员",json + "");
                //MapDataBean_DailiShang installListBean = GsonUtil.GsonToBean(json, MapDataBean_DailiShang.class);

               InstallListBean installListBean = GsonUtil.GsonToBean(json, InstallListBean.class);
                if (installListBean != null) {
                    if (installListBean.status==1) {

                        List<InstallListBean.DataBean> data = installListBean.data;
                        if (data != null) {
                            data1.clear();
                            data2.clear();
                            data4.clear();
                            for (int i = 0; i < data.size(); i++) {
                                InstallListBean.DataBean dataBean = data.get(i);
                                if (dataBean.is_sign.equals("1")) {
                                    data1.add(dataBean);
                                } else if (dataBean.is_sign.equals("2")) {
                                    data2.add(dataBean);
                                } else if (dataBean.is_sign.equals("4")) {
                                    data4.add(dataBean);
                                }
                            }


                            if (data1.size() > 0) {
                                mTextView1.setVisibility(View.VISIBLE);
                                mInstalllistRc.setVisibility(View.VISIBLE);
                            } else {
                                mTextView1.setVisibility(View.GONE);
                                mInstalllistRc.setVisibility(View.GONE);
                            }


                            if (data2.size() > 0) {
                                mTextView2.setVisibility(View.VISIBLE);
                                mInstalllistRc2.setVisibility(View.VISIBLE);
                            } else {
                                mTextView2.setVisibility(View.GONE);
                                mInstalllistRc2.setVisibility(View.GONE);
                            }


                            if (data4.size() > 0) {
                                mTextView4.setVisibility(View.VISIBLE);
                                mInstalllistRc4.setVisibility(View.VISIBLE);
                            } else {
                                mTextView4.setVisibility(View.GONE);
                                mInstalllistRc4.setVisibility(View.GONE);
                            }

                          /*  datas.addAll(data);*/
                            mInstallListAdapter.notifyDataSetChanged();
                            mInstallListAdapter2.notifyDataSetChanged();
                            mInstallListAdapter4.notifyDataSetChanged();
                        }

                    }
                }
            }
        });
    }


    @Override
    protected void onResume() {
        super.onResume();
//        getInstallData();
    }
}
